<template>
<el-container>
  <el-header>后台管理系统</el-header>
  <el-container>
    <el-aside width="200px">
        <el-menu
      default-active="2"
      class="el-menu-vertical-demo"
      @open="handleOpen"
      @close="handleClose"
      background-color="#545c64"
      text-color="#fff"
      active-text-color="#ffd04b">
      <el-submenu index="1">
        <template slot="title">
          <i class="el-icon-location"></i>
          <span>用户管理</span>
        </template>
        <el-menu-item-group>
          <el-menu-item index="1-1">用户添加</el-menu-item>
          <el-menu-item index="1-2">用户权限</el-menu-item>
        </el-menu-item-group>
      </el-submenu>
      <el-menu-item index="2">
        <i class="el-icon-menu"></i>
        <span slot="title">课程管理</span>
      </el-menu-item>
    </el-menu>
    </el-aside>
    <el-main>
 <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>

<div class="add-form">
                <el-dialog title="新增学生" :visible.sync="dialogFormVisible">
                    <el-form :model="formData" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                           <el-col :span="22">
                            <el-form-item label="性别" prop="gender">
                                <el-input v-model="formData.gender"/>
                            </el-form-item>
                        </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="密码" prop="password">
                                    <el-input v-model="formData.password"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="年龄" prop="age">
                                    <el-input v-model="formData.age"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                         <el-row>
                            <el-col :span="22">
                                <el-form-item label="电话" prop="tel">
                                    <el-input v-model="formData.tel"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

 <div class="edit-form">
                <el-dialog title="编辑学生" :visible.sync="dialogFormVisible4Edit">
                    <el-form   :model="formData" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                           <el-col :span="22">
                            <el-form-item label="性别" prop="gender">
                                <el-input v-model="formData.gender"/>
                            </el-form-item>
                        </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="密码" prop="password">
                                    <el-input v-model="formData.password"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="年龄" prop="age">
                                    <el-input v-model="formData.age"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                         <el-row>
                            <el-col :span="22">
                                <el-form-item label="电话" prop="tel">
                                    <el-input v-model="formData.tel"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
      
        <el-table
    :data="tableData.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
    style="width: 100%">
    <el-table-column
      label="员工号"
      prop="id">
    </el-table-column>
    <el-table-column
      label="姓名"
      prop="name">
    </el-table-column>
    <el-table-column
      label="性别"
      prop="gender">
    </el-table-column>
    <el-table-column
      label="密码"
      prop="password">
    </el-table-column>
    <el-table-column
      label="年龄"
      prop="age">
    </el-table-column>
    <el-table-column
      label="电话"
      prop="tel">
    </el-table-column>
    <el-table-column
      align="right">
      <template slot="header" slot-scope="scope">
        <el-input
          v-model="search"
          size="mini"
          placeholder="输入关键字搜索"/>
      </template>
      <template slot-scope="scope">
        <el-button
          size="mini"
          type="warning"
          @click="findById(scope.$index, scope.row.id)">修改</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.$index, scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
    </el-main>
  </el-container>
</el-container>
</template>
<script>
export default {
    name: "index",
    data() {
      return {
        tableData: [],
        search: '',
         dialogFormVisible: false,//增加表单是否可见
         dialogFormVisible4Edit:false,//编辑表单是否可见
          formData: {},//表单数据
      }
    },
    methods: {
      handleAdd(key,keyPath){
          this.axios.post("http://localhost:8081/emp/saveOne",this.formData).then(res =>{
            alert("添加成功")
             this.dialogFormVisible=false;
            this.findAll()
          })
      },
    handleEdit(key,keyPath){
          this.axios.post("http://localhost:8081/emp/updateOne",this.formData).then(res =>{
            alert("修改成功")
              this.dialogFormVisible4Edit=false;
            this.findAll()
          })
      },
      handleCreate() {
                // 如果只更新了html，ctrl+f9
				  this.dialogFormVisible=true;
                this.formData = {};
            },
      handleOpen(key, keyPath) {
        console.log(key, keyPath);
      },
      handleClose(key, keyPath) {
        console.log(key, keyPath);
        },
      handleEdit(index, row) {
        console.log(index, row);
      },
      handleDelete(index, row) {
       this.axios.get("http://localhost:8081/emp/del?id="+row).then(res =>{
             this.findAll()
        })
      },
      findAll(){
        this.axios.get('http://localhost:8081/emp/findAll').then(res=>{
          this.tableData=res.data
          console.log(res.data)
        })
      },
      handleUpdate(row) {
                // 数据回显
                this.dialogFormVisible4Edit=true;
                
              this.findById()
            }
  },
      findById(index,row){
          this.axios.get('http://localhost:8081/emp/findById?id='+row).then(res=>{
          this.formData=res.data
          console.log(res.data)
        })
      },
    created(){
      console.log("一道该页面")
      this.findAll(),
      this.findById()
    }
    
}
</script>
<style scoped>
.el-container{
    height: 920px;
}
.el-header, .el-footer {
    background-color: #B3C0D1;
    color: #333;
    text-align: center;
    line-height: 60px;
  }
  
  .el-aside {
    background-color: #545c64;
    color: #333;
    text-align: center;
    line-height: 200px;
  }
  
  .el-main {
    background-color: #E9EEF3;
    color: #333;
    text-align: center;
    line-height: 10px;
  }
  
  body > .el-container {
    margin-bottom: 40px;
  }
  
  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }
  
  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }
</style>